/*
 * Copyright (C) 2014  Forklabs Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.trello.client;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import ca.forklabs.baselib.util.Algorithm;
import ca.forklabs.baselib.util.Iterators;
import ca.forklabs.baselib.util.UnaryPredicate;
import ca.forklabs.trello.client.model.Board;
import ca.forklabs.trello.client.model.Card;

public class GroceryListSpike {

   private static UnaryPredicate<Card> upToOneWeekFromNow() {
      final long now = System.currentTimeMillis();
      final long in_one_week = now + TimeUnit.DAYS.toMillis(7L);

      final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
      df.setTimeZone(TimeZone.getTimeZone("UTC"));

      UnaryPredicate<Card> predicate = new UnaryPredicate<Card>() {
         @Override
         public boolean eval(Card card) {
            boolean up_to_one_week_from_now;

            String due = card.due;

            try {
               Date date = df.parse(due);
               long millis = date.getTime();
               up_to_one_week_from_now = (now <= millis) && (in_one_week >= millis);
               }
            catch (ParseException | NullPointerException e) {
               up_to_one_week_from_now = false;
               }
            return up_to_one_week_from_now;
            }
         };
      return predicate;
      }

   public static void main(String... args) throws Exception {
      if (1 > args.length) {
         System.out.println("java ca.forklabs.trello.client.GroceryListSpike <trello key>");
         }

      String key = args[0];
      TrelloClient client = new TrelloClient(key);

      String board_id = "542b0bd40d309dc6eba7ec89";
      Board board = new Board(board_id);

      Card[] cards = client.cards(board);

      List<Card> candidates = new LinkedList<>();
      Algorithm.copyIf(Iterators.asIterator(cards), Iterators.backInserter(candidates), upToOneWeekFromNow());

      List<String> ingredients = new LinkedList<>();
      for (Card card : candidates) {
         String desc = card.desc;

         if (desc.trim().startsWith("##I")) {
            int first_dash = desc.indexOf('-');
            int procedure = desc.indexOf("##P");
            if (-1 == procedure) {
               procedure = desc.indexOf("##D");
               }
            String text = desc.substring(first_dash, procedure).trim();

            String[] mats = text.split("- ");
            for (String mat : mats) {
               if (false == "".equals(mat)) {
                  ingredients.add(mat.trim());
                  }
               }
            }
         }

      System.out.println("Il y a " + ingredients.size() + " ingredients.");
      for (String ingredient : ingredients) {
         System.out.println(ingredient);
         }
      }

   }
